package com.ruoyi.web.controller.farmer;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.farmer.domain.Sku;
import com.ruoyi.farmer.service.ISkuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 农产品商品管理Controller
 *
 * @author 焦博兴
 * @date 2023-09-13
 */
@RestController
@RequestMapping("/sku/sku")
public class SkuController extends BaseController
{
    @Autowired
    private ISkuService skuService;

    /**
     * 查询农产品商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('sku:sku:list')")
    @GetMapping("/list")
    public TableDataInfo list(Sku sku)
    {
        startPage();
        List<Sku> list = skuService.selectSkuList(sku);
        System.out.println(skuService.selectSkuList(sku));
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('sku:sku:listsSku')")
    @GetMapping(value = "/listsSku")
    public AjaxResult listsSku()
    {

        return success(skuService.listsSku());
    }


    /**
     * 导出农产品商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('sku:sku:export')")
    @Log(title = "农产品商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Sku sku)
    {
        List<Sku> list = skuService.selectSkuList(sku);
        ExcelUtil<Sku> util = new ExcelUtil<Sku>(Sku.class);
        util.exportExcel(response, list, "农产品商品管理数据");
    }

    /**
     * 获取农产品商品管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('sku:sku:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {

        return success(skuService.selectSkuById(id));
    }

    /**
     * 新增农产品商品管理
     */
    @PreAuthorize("@ss.hasPermi('sku:sku:add')")
    @Log(title = "农产品商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Sku sku)
    {
        sku.setSkuId(UUID.randomUUID().toString());
        sku.setCreateTime(DateUtils.getTime());
        return toAjax(skuService.insertSku(sku));
    }

    /**
     * 修改农产品商品管理
     */
    @PreAuthorize("@ss.hasPermi('sku:sku:edit')")
    @Log(title = "农产品商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Sku sku)
    {
        sku.setUpdateTime(DateUtils.getTime());
        return toAjax(skuService.updateSku(sku));
    }

    /**
     * 删除农产品商品管理
     */
    @PreAuthorize("@ss.hasPermi('sku:sku:remove')")
    @Log(title = "农产品商品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(skuService.deleteSkuByIds(ids));
    }
}
